package com.lzx.iteam.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.AttendanceClassData;
import com.lzx.iteam.util.DateUtil;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 12月 09日
 * Time: 上午10:52
 *
 * @QQ : 1234567890
 * **********************************
 */
public class MySubClassAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<AttendanceClassData> mData;
    private long mTodayDate;

    public MySubClassAdapter(Context mContext) {
        this.mContext = mContext;
        mTodayDate = DateUtil.getDayBegin();
    }

    public void bindData(ArrayList<AttendanceClassData> mData){
        this.mData = mData;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData == null ? 0:mData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        AttendanceClassData classData = mData.get(position);
        ViewHolder holder = null;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.at_my_sub_item_layout,null);
            holder.line = convertView.findViewById(R.id.view_my_sub_item);
            holder.ivCircle = (ImageView) convertView.findViewById(R.id.iv_my_sub_item_circle);
            holder.tvWeek = (TextView) convertView.findViewById(R.id.tv_my_sub_week);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tv_my_sub_time);
            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        if (position % 2 == 0){
            convertView.setBackgroundColor(mContext.getResources().getColor(R.color.frag_bg));
        }else{
            convertView.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        }

        long date = classData.getDate();
        if (date < mTodayDate){
            holder.tvWeek.setTextColor(mContext.getResources().getColor(R.color.color_999999));
            holder.tvTime.setTextColor(mContext.getResources().getColor(R.color.color_999999));
            holder.line.setVisibility(View.GONE);
            holder.ivCircle.setImageDrawable(mContext.getResources().getDrawable(R.drawable.iv_circle_gray));

        }else if (date == mTodayDate){
            holder.tvWeek.setTextColor(mContext.getResources().getColor(R.color.color_333333));
            holder.tvTime.setTextColor(mContext.getResources().getColor(R.color.color_333333));
            holder.line.setVisibility(View.VISIBLE);
            holder.ivCircle.setImageDrawable(mContext.getResources().getDrawable(R.drawable.iv_circle_blue));
        }else{
            holder.tvWeek.setTextColor(mContext.getResources().getColor(R.color.color_333333));
            holder.tvTime.setTextColor(mContext.getResources().getColor(R.color.color_333333));
            holder.line.setVisibility(View.GONE);
            holder.ivCircle.setImageDrawable(mContext.getResources().getDrawable(R.drawable.iv_circle_gray));
        }
        holder.tvWeek.setText(DateUtil.getSubDateTitle2(date * 1000));
        String title = "";
        if (classData.getIs_quartic() == 0){
            title = classData.getSub_name() + "\n" + classData.getStart_work_time() + "  -  " + checkTime(classData.getEnd_work_time());
        } else if (classData.getIs_quartic() == 1){
            title  = classData.getSub_name() + "\n" + classData.getStart_work_time() + " - " + checkTime(classData.getNoon_end_time()) + "  " +checkTime(classData.getNoon_start_time()) + " - " +checkTime(classData.getEnd_work_time());
        }
        holder.tvTime.setText(title);
        return convertView;
    }

    private String checkTime(String time){
        if (time.contains("-")){
            return time.replace("-","次日");
        }else{
            return time;
        }
    }

    class ViewHolder{
        View line;
        ImageView ivCircle;
        TextView tvWeek;
        TextView tvTime;

    }
}
